Constructor And Destructor

 Constructor And Destructor

Introduction-



  •  constructor is a very special member function whose name is same as class name. consturtor is self executable when object is created. consturctor is always declare inside a public mode. the role of the consturcotr is inilized the value.
  •  constructor is not required any kind of return type not even void

Type of constuctor-

  • constructe can be categoriese in 4 type-
        1) default
        2) non paramertized
        3) paramerized constructor
        4) copy cocnstrucor

Parameterized constructors-

  •  It may be necessary to initialize the various data elements of different objects with different values when they are created. This is achieved by passing arguments to the constructor function when the objects are created. The constructors that can take arguments are called parameterized constructors.

Multiple constructors in a classclass-

        number
        {
        int a,b;
        public :
        number ()
        {
        a=0;
        b=0;
        }
        void show()
        {
        cout<< a<<b;
        };

Constructors with default arguments-

  •  Default arguments- As the name says, it is a function argument with pre-specified value. For example, in function declaration 'void foo(int x, int y=10)', argument y has default value of 10. This also means that when you call this function, you have a choice of not passing value for y and its value will be taken as 10 inside the function foo.
  •  Default Constructor- As per C++ language, a constructor is considered to be default one if it does not have any arguments or it has arguments, but all the arguments have default values. As an example for a class named Foo, the following constructors will be default one.
    Foo();
    or
    Foo(int x=10, int y=30);
    In both the cases, even if you don't provide parameter values, you can get a valid object. As a follow up exercise, try putting both the above constructors in a class and try creating an object without passing any parameter. Find out what compiler has to say about it.

Dynamic initialization of objects-

 Dynamic initialization of object refers to initializing the objects at run time i.e. the initial value of an object is to be provided during run time. Dynamic initialization can be achieved using constructors and passing parameters values to the constructors.
                            #include<isotram.h>
                            class number
                            {
                            int a,b;
                            public :
                            number(int i, int j)
                            {
                            a=i;
                            b=j'
                            }
                            void show()
                            {
                            cout<<a<<b;
                            }
                            void main()
                            {
                            number n(10,20);
                            n.show();
                            getch();
                            }

Copy constructor-

  •  A copy constructor is a member function which initializes an object using another object of the same class.
  •  A copy constructor has the following general function prototype:

                        ClassName (const ClassName &old_obj);
                        Following is a simple example of copy constructor.

                        #include<iostream>
                        using namespace std;

                        class Point
                        {
                         private:
                            int x, y;
                        public:
                            Point(int x1, int y1) { x = x1; y = y1; }

                            // Copy constructor
                            Point(const Point &p2) {x = p2.x; y = p2.y; }

                            int getX() { return x; }
                            int getY() { return y; }
                        };

                        int main()
                        {
                            Point p1(10, 15); // Normal constructor is called
                        here
                            Point p2 = p1; // Copy constructor is called here

                            // Let us access values assigned by constructors
                            cout << "p1.x = " << p1.getX() << ", p1.y = " <<
                    p1.getY();
                        cout << "\np2.x = " << p2.getX() << ", p2.y = " <<
                    p2.getY();
                        return 0;
                    }

Destructors-

  •  A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
  • A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
                class Line {
                public:
                void setLength( double len );
                double getLength( void );
                Line(); // This is the constructor declaration
                ~Line(); // This is the destructor: declaration
                private:
                double length;
                };

Operator Overloading

Post a Comment

Previous Post Next Post